package com.example.performance.jol.official;

import org.openjdk.jol.datamodel.DataModel;
import org.openjdk.jol.datamodel.Model32;
import org.openjdk.jol.datamodel.Model64;
import org.openjdk.jol.info.ClassLayout;
import org.openjdk.jol.layouters.CurrentLayouter;
import org.openjdk.jol.layouters.HotSpotLayouter;
import org.openjdk.jol.layouters.Layouter;

/**
 * @author Aleksey Shipilev
 */
@SuppressWarnings({"java:S101", "java:S106", "unused"})
public class JOLSample_10_DataModels {

    /*
     * This example shows the differences between the data models.
     * First layout is the actual VM layout, everything else are
     * simulations.
     *
     * JDK 15 brought a new layouter, which may pack fields more densely.
     * JDK 15 also allows to have compressed class pointers without
     * compressed oops.
     *
     * You can see the reference sizes are different, depending on VM model:
     *  - with 32-bit model, reference size is 4 bytes
     *  - with 64-bit model, reference size is 8 bytes
     *      unless compressed references are enabled
     *      (enabled by default when heap size is small)
     *
     * The mark word sizes depend on JVM bitness:
     *  - with 32-bit model, mark word size is 4 bytes
     *  - with 64-bit model, mark word size is 8 bytes
     *
     * The class word sizes depend on JVM model:
     *  - with 32-bit model, class word is 4 bytes
     *  - with 64-bit model, class word is 8 bytes
     *     unless compressed class pointers are enabled
     *     (enabled by default when compressed references are enabled)
     *     (since JDK 15, can be enabled even without compressed references)
     */

    private static final DataModel[] MODELS_JDK8 = new DataModel[]{
            new Model32(),
            new Model64(false, false),
            new Model64(true, true),
            new Model64(true, true, 16),
    };

    private static final DataModel[] MODELS_JDK15 = new DataModel[]{
            new Model64(false, true),
            new Model64(false, true, 16),
    };

    private static final String SEPARATE_LINE = "========================== ";


    public static void main(String[] args) {

        Layouter currentLayouter = new CurrentLayouter();
        System.out.println(SEPARATE_LINE + currentLayouter);
        System.out.println(ClassLayout.parseClass(A.class, currentLayouter).toPrintable());


        for (DataModel model : MODELS_JDK8) {
            Layouter l = new HotSpotLayouter(model, 8);
            System.out.println(SEPARATE_LINE + l);
            System.out.println(ClassLayout.parseClass(A.class, l).toPrintable());
        }

        for (DataModel model : MODELS_JDK8) {
            Layouter l = new HotSpotLayouter(model, 15);
            System.out.println(SEPARATE_LINE + l);
            System.out.println(ClassLayout.parseClass(A.class, l).toPrintable());
        }

        for (DataModel model : MODELS_JDK15) {
            Layouter l = new HotSpotLayouter(model, 15);
            System.out.println(SEPARATE_LINE + l);
            System.out.println(ClassLayout.parseClass(A.class, l).toPrintable());
        }
    }

    public static class A {
        Object fieldA;
        int fieldB;
    }

}